package tw.com.hitevision.whiteboard.android.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import tw.com.hitevision.whiteboard.android.MainActivity;
import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.navigator.Item;
import tw.com.hitevision.whiteboard.android.state.WhiteboardState;
import tw.com.hitevision.whiteboard.android.util.TextLayoutUtil;

public class NavigatorViewQRCodeExportDialog implements DialogInterface {

    private String TAG = NavigatorViewQRCodeExportDialog.class.getSimpleName();
    private Context context;
    private Activity activity;
    private Dialog dialog;
    private List<WhiteboardState> whiteboardStateList;
    private FileFormat currentExportFileFormat;
    private View isSelected;
    private TextView selectedText;
    private Handler handler;
    private List<String> currentFilePathList = new ArrayList<>();
    private NavigatorViewQRCodeDownloadDialog navigatorViewQRCodeDownloadDialog;
    private NavigatorViewQRCodeFakeSpinner navigatorViewQRCodeFakeSpinner;
    private View share;
    private List<Item> itemFileList;
    private ArrayList<String> exportFileSelectorList = new ArrayList<>();

    public enum FileFormat {
        PNG,  JPEG,PDF
    }

    public NavigatorViewQRCodeExportDialog(Activity activity, List<WhiteboardState> whiteboardStateList, NavigatorViewQRCodeDownloadDialog navigatorViewQRCodeDownloadDialog, List<Item> fileList) {
        dialog = new Dialog(activity, R.style.ExportDialog);
//        this.currentFilePathList = fileList;
//        this.context = context;
        this.whiteboardStateList = whiteboardStateList;
        this.itemFileList = fileList;
        addCurrentFilePathFromList(fileList);
        this.activity = activity;
        this.navigatorViewQRCodeDownloadDialog = navigatorViewQRCodeDownloadDialog;
        currentExportFileFormat = FileFormat.PNG;
        exportFileSelectorList.add(activity.getResources().getString(R.string.options_export_export_as_png_option));
        exportFileSelectorList.add(activity.getResources().getString(R.string.options_export_export_as_jpeg_option));
        exportFileSelectorList.add(activity.getResources().getString(R.string.options_export_export_as_pdf_option));

        handler = new Handler();
    }

    public void show() {
        dialog.setContentView(R.layout.navigator_qrcode_export_dialog_layout);
        if(Props.isFake4k()) {
            Log.d("QrcodeExportDialog","811");
            WindowManager.LayoutParams layoutParams = Objects.requireNonNull(dialog.getWindow()).getAttributes();
            layoutParams.setTitle("ist_win_editdialog");
            dialog.getWindow().setAttributes(layoutParams);
        }
        Objects.requireNonNull(dialog.getWindow()).setGravity(Gravity.CENTER);
        Objects.requireNonNull(dialog.getWindow()).getAttributes().y = Math.round(TextLayoutUtil.dp2px(activity, -24));
        Objects.requireNonNull(dialog.getWindow()).setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.setOnCancelListener(dialogInterface -> cancel());
        dialog.setOnDismissListener(dialogInterface -> dismiss());
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        dialog.getWindow().setDimAmount(0.5f);
        View parentLayout =dialog.findViewById(R.id.navigator_export_parent_layout);
        View cancel_view = dialog.findViewById(R.id.navigator_qrcode_export_cancel);
        cancel_view.setOnClickListener(view -> cancel());
        View ok_view = dialog.findViewById(R.id.navigator_qrcode_export_ok);
        ok_view.setOnClickListener(view -> {
            cancel();
            NavigatorViewQRCodeDownloadDialog navigatorViewQRCodeDownloadDialog = new NavigatorViewQRCodeDownloadDialog(activity, handler);
            navigatorViewQRCodeDownloadDialog.setData(currentFilePathList, currentExportFileFormat);
            navigatorViewQRCodeDownloadDialog.show();
        });

        isSelected = dialog.findViewById(R.id.navigator_qrcode_export_option_selected_layout);
        selectedText = dialog.findViewById(R.id.navigator_qrcode_export_option_selected_text);

        isSelected.setOnClickListener(view -> {
            Log.d(TAG, "asFakeSpinnerOnClick");
            if(!isSelected.isActivated()) {
//                arrow.setImageResource(R.drawable.ic_arrow_up);
                navigatorViewQRCodeFakeSpinner.setCurrentPagesType(currentExportFileFormat);
                int[] asLocationInWindowPoint = new int[2];
                selectedText.getLocationInWindow(asLocationInWindowPoint);
                navigatorViewQRCodeFakeSpinner.show(parentLayout.getLayoutParams().width, parentLayout.getHeight(), asLocationInWindowPoint[0], asLocationInWindowPoint[1], isSelected.getMeasuredWidth());
            } else {
//                arrow.setImageResource(R.drawable.ic_arrow_down);
                navigatorViewQRCodeFakeSpinner.dismiss();
            }
        });


        navigatorViewQRCodeFakeSpinner = new NavigatorViewQRCodeFakeSpinner(activity, exportFileSelectorList, currentExportFileFormat, new NavigatorViewQRCodeFakeSpinner.ItemOnClickListener() {
            @Override
            public void itemOnClicked(int position) {
                currentExportFileFormat = FileFormat.values()[position];
                selectedText.setText(exportFileSelectorList.get(position));
            }

            @Override
            public void dialogDismiss() {
            }
        });

        dialog.show();
    }


    private void addAllCurrentFilePathList() {
        currentFilePathList.clear();
//        dropDownLayout.setVisibility(View.GONE);
//        exportButton.setBackground(activity.getResources().getDrawable(R.drawable.options_export_enable_export_button_background));
        for(int i=0; i<whiteboardStateList.size(); i++) {
            currentFilePathList.add(i, whiteboardStateList.get(i).getPreviewFile());
        }
    }

    private void addCurrentFilePathFromList(List<Item> list) {
        currentFilePathList.clear();
        for(int i=0; i<list.size(); i++) {
            currentFilePathList.add(list.get(i).getPreviewFile());
        }
    }

    @Override
    public void cancel() {
        if(dialog.isShowing()) {
            dialog.cancel();
        }
    }

    @Override
    public void dismiss() {
        if(dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
